package com.example.testproject.adapter

import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.chad.library.adapter.base.animation.BaseAnimation
import com.example.testproject.R
import com.example.testproject.base.BaseRecyclerViewTemplate
import com.example.testproject.bean.SelectBean
import com.example.testproject.ext.bold
import com.example.testproject.ext.click

/**
 *
 * @ClassName: SelectDepartmentAdapter
 * @Author: android 超级兵
 * @CreateDate: 12/17/21$ 11:11 AM$
 * TODO
 */
open class SelectDepartmentAdapter(data: List<List<SelectBean.Data>>?) :
    BaseQuickAdapter<List<SelectBean.Data>, BaseViewHolder>(R.layout.select_department, data) {

    var childAdapter: SelectDepartmentChildAdapter? = null

    override fun convert(helper: BaseViewHolder, item: List<SelectBean.Data>) {

        SelectDepartmentChildTemplate(item).apply {
            val rvSelect = helper.itemView.findViewById<RecyclerView>(R.id.rvSelect)
            template(mContext, rvSelect).apply {
                childAdapter = getAdapter() as SelectDepartmentChildAdapter

                childAdapter?.onClick =
                    SelectDepartmentChildAdapter.OnSelectDepartmentChildListener { position, item ->
                        onParentClick?.itemCallBack(helper.adapterPosition, position, item)
                    }
            }
        }
    }

    fun interface OnSelectDepartmentParentListener {
        fun itemCallBack(parentPosition: Int, childPosition: Int, item: SelectBean.Data)
    }

    var onParentClick: OnSelectDepartmentParentListener? = null
}

class SelectDepartmentChildTemplate(val data: List<SelectBean.Data>) :
    BaseRecyclerViewTemplate() {
    private lateinit var adapter: SelectDepartmentChildAdapter
    override fun buildAdapter() = let {
        SelectDepartmentChildAdapter(data)
    }.apply { adapter = this }

    override fun getAdapter(): RecyclerView.Adapter<*> = adapter

    override fun getAnimation(): BaseAnimation {
        return BaseAnimation { arrayOfNulls(0) }
    }
}

class SelectDepartmentChildAdapter(data: List<SelectBean.Data>?) :
    BaseQuickAdapter<SelectBean.Data, BaseViewHolder>(R.layout.item_recycler_view, data) {
    override fun convert(helper: BaseViewHolder, item: SelectBean.Data) {

        helper.itemView.findViewById<TextView>(R.id.tv).apply {
            text = item.name

            bold()

            setTextColor(mContext.resources.getColor(if (item.isSelect) R.color.shrine_pink_50 else R.color.black))

            click {
                onClick?.itemCallBack(helper.adapterPosition, item)
            }
        }
    }


    fun interface OnSelectDepartmentChildListener {
        fun itemCallBack(position: Int, item: SelectBean.Data)
    }

    var onClick: OnSelectDepartmentChildListener? = null
}